package com.ruoyi.common.json;


import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description: http://www.easysb.cn/2019/07/482.html
 * @Date: 2019/7/16
 * @Author: Jekkay Hu
 */

@Slf4j
public class JsonMap extends LinkedHashMap<String, Object> {

    public static JsonMap toJsonMap(Map<String, Object> map) {
        JsonMap jsonMap = new JsonMap();
        jsonMap.putAll(map);
        return jsonMap;
    }

    public JsonMap removeEndWith(String endWith) {
        Set<String> keys = this.keySet();
        for (String key : keys) {
            if (key.endsWith(endWith)) {
                this.remove(key);
            }
        }
        return this;
    }


    public static JsonMap paramToJsonMap(Map<String, String> map) {
        JsonMap jsonMap = new JsonMap();
        for (Map.Entry<String, String> m : map.entrySet()) {
            if (m.getValue() != null && !"null".equals(m.getValue())) {
                jsonMap.put(m.getKey(), m.getValue());
            }
        }
        return jsonMap;
    }

    public static JsonMap paramNotBlankToJsonMap(Map<String, String> map) {
        JsonMap jsonMap = new JsonMap();
        for (Map.Entry<String, String> m : map.entrySet()) {
            if (StringUtils.isNotBlank(m.getValue())) {
                jsonMap.put(m.getKey(), m.getValue());
            }
        }
        return jsonMap;
    }


    public Long getLong(String key) {
        return JSONObject.valueAsLong(this.get(key));
    }

    public BigDecimal getBigDecimal(String key) {
        return JSONObject.valueAsBigDecimal(this.get(key));
    }


    public Boolean getBool(String key) {
        return JSONObject.valueAsBool(this.get(key));
    }


    public String getString(String key) {
        return JSONObject.valueAsStr(this.get(key));
    }

    public Integer getInt(String key) {
        return JSONObject.valueAsInt(this.get(key));
    }

    public long getLongVal(String key) {
        Long aLong = getLong(key);
        return aLong == null ? 0L : aLong;
    }

    public int getIntVal(String key) {
        Integer integer = getInt(key);
        return integer == null ? 0 : integer;
    }

    public boolean getBooleanVal(String index) {
        Boolean value = this.getBool(index);
        return value != null && value;
    }


    public JsonMap getJsonMap(String key) {
        Object value = this.get(key);
        return JsonUtil.toJsonMap(value);
    }

    public JsonList getJsonList(String key) {
        Object value = this.get(key);
        return JsonUtil.toJsonList(value);
    }

    public <T> T getObject(String key, Class<T> clazz) {
        Object obj = this.get(key);
        return JsonUtil.toJavaObject(obj, clazz);
    }

    public <T> T toJavaObject(Class<T> clazz) {
        return JsonUtil.toJavaObject(this, clazz);
    }

    @Override
    public String toString() {
        return JsonUtil.toJSONString(this);
    }
}